//
//  NSString+GJExtention.m
//  xmgjAppFront
//
//  Created by Zeki on 2020/7/9.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "NSString+GJExtention.h"

@implementation NSString (GJExtention)
- (BOOL)gj_hasValue {
    if ([self isEqualToString:@"null"] ||
        [self isEqualToString:@""] ||
        [self isEqualToString:@"(null)"]) {
        return NO;
    } else {
        return YES;
    }
}
- (NSMutableAttributedString *)gj_setLineSpace:(CGFloat)space{
  NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:self];
  NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
  paragraph.lineSpacing = space;
  [attrText addAttribute:NSParagraphStyleAttributeName value:paragraph range:attrText.string.rangeOfAll];
  return attrText;
}
- (NSString *)gj_queryURLEncode {
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

- (CGRect)gj_measureFrameWithFont:(UIFont *)font size:(CGSize)size{
    NSDictionary *attriDic = @{NSFontAttributeName : font};
    CGRect strRect = [self boundingRectWithSize:size
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:attriDic
                                        context:nil];
    return strRect;
}
- (NSDictionary *)resolvingUrlPathAndParameters{
    
    NSURL *urlBase = [NSURL URLWithString:[self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
    
    //1.先按照‘&’拆分字符串
    NSArray *array = [urlBase.query componentsSeparatedByString:@"&"];
    //2.初始化两个可变数组
    NSMutableArray *mutArrayKey = [[NSMutableArray alloc]init];
    NSMutableArray *mutArrayValue = [[NSMutableArray alloc]init];
    //3.以拆分的数组内容个数为准继续拆分数组，并将拆分的元素分别存到两个可变数组中
    for (int i=0; i<[array count]; i++) {
        NSArray *arr = [array[i] componentsSeparatedByString:@"="];
        [mutArrayKey addObject:arr[0]];
        [mutArrayValue addObject:arr[1]];
    }
    //4.初始化一个可变字典，并设置键值对
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects:mutArrayValue forKeys:mutArrayKey];
    
    return @{@"scheme":urlBase.scheme?:@"",
             @"host":urlBase.host?:@"",
             @"port":urlBase.port?:@"",
             @"path":urlBase.path?:@"",
             @"query":dict};
    
}
@end
